/*
 * @Description: 针对业务的全局方法，用于在品类改变时触发的搜索函数
 * @Author: youzi
 * @Date: 2020-07-21 11:42:52
 * @LastEditors: youzi
 * @LastEditTime: 2020-09-29 11:14:55
 */

const api = {
  // 获取供应商数据
  getAllSuppliers: "/supplier/getAllSuppliers"
};
import http from "./request";
/**
 * @desc: 小品类级联，调用api查询供应商列表
 * @param {boolean} isInBlacklist 0: 非黑名单数据；1：黑名单数据；3：所有数据 4: 所有数据且传大品类
 * @return:
 * @author: youzi
 */
export const onMediumIdChange = async (
  productTypeMediumId,
  productTypeLargeId,
  isInBlacklist = 0
) => {
  let list = [];
  // debugger
  var params = {};
  if (isInBlacklist == 4) {
    params = {
      productTypeLargeId: productTypeLargeId,
      isInBlacklist: 3
    }
  } else {
    params = {
      productTypeMediumId: productTypeMediumId,
      isInBlacklist: isInBlacklist
    }
  }
  await http
    .post(api.getAllSuppliers, params)
    .then(r => {
      let d = r.data.data;
      list = d.map(el => {
        return {
          id: el.id,
          supplierName: el.supplierName,
          value: el.id,
          label: el.supplierName
        };
      });
    })
    .catch(err => {
      console.error(err);
    });
  return list;
};

/**
 * @desc: 大品类选择框改变时的函数，同步更新小品类集合的数据
 * @param {ojbect} value 选中的value值
 * @author: youzi
 */
export const onLargeIdChange = (value, productTypeLargeId) => {
  for (let i = 0; i < productTypeLargeId.length; i++) {
    const el = productTypeLargeId[i];
    console.log(value);
    if (el.id === value) {
      return el.child;
    }
  }
};
